package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.HomepageAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.CoinBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.fragment.JifentixianFragment;
import com.luoxiang.socialandroid.fragment.LiveCoinFragment;
import com.luoxiang.socialandroid.fragment.PayFragment;
import com.luoxiang.socialandroid.fragment.SeaPayFragment;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.CustomClickListener;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SeaWalletActivity extends BaseActivity {
    private ImageView back;
    private TabLayout tab_home;
    private ViewPager vp_wallet;
    private ArrayList<String> list = new ArrayList<>();
    private ArrayList<Fragment> fragmentlist = new ArrayList<>();
    private String token;
    private TextView tv_coincount, tv_jifen, tv_mybill, zhubooryonghu;
    private int isHost;
    private ImageView yaoqingyouli;

    @Override
    protected void initListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tv_mybill.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SeaWalletActivity.this, MybillActivity.class);
                intent.putExtra("ishost", isHost);
                startActivity(intent);
            }
        });
        yaoqingyouli.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                //跳转
                Intent intent = new Intent(SeaWalletActivity.this, RecommendActivity.class);
                startActivity(intent);
            }

            @Override
            protected void onFastClick() {

            }
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_sea_wallet);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        back = findViewById(R.id.back);
        tab_home = findViewById(R.id.tab_wallet);
        vp_wallet = findViewById(R.id.vp_wallet);
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        LogUtils.i("token", token);
        tv_coincount = findViewById(R.id.tv_coincount);
        tv_jifen = findViewById(R.id.tv_jifen);
        tv_mybill = findViewById(R.id.tv_mybill);
        zhubooryonghu = findViewById(R.id.zhubooryonghu);
        yaoqingyouli = findViewById(R.id.yaoqingyouli);
        LiveEventBus.get("tixianshuaxin").observe(this, o -> {
                    getMyWallet(EXTRA, token);
                }

        );

    }

    @Override
    protected void onResume() {
        super.onResume();
        getRequest();
        getMyWallet(EXTRA, token);

    }

    private void getMyWallet(String extra, String token) {
        NetUtils.getInstance().getApiService().getMyWallet(extra, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        com.blankj.utilcode.util.LogUtils.i("decryptStr199", decryptStr);
                        Gson gson = new Gson();
                        CoinBean coinBean = gson.fromJson(decryptStr, CoinBean.class);
                        if (coinBean != null) {
                            if (coinBean.getData() != null) {
                                LiveEventBus.get("refreshData").post(null);
                                tv_jifen.setText(getString(R.string.mi_wodejifen2) + coinBean.getData().getCredit());
                                MmkvUtil.INSTANCE.encode("isVip", coinBean.getData().isVip());
                                isHost = coinBean.getData().isHost();
                                LogUtils.i("ishost", isHost);
                                vp_wallet.setOnTouchListener(new View.OnTouchListener() {

                                    @Override
                                    public boolean onTouch(View v, MotionEvent event) {
                                        return true;
                                    }
                                });
                                list.clear();
                                fragmentlist.clear();

                                if (isHost == 0) {
                                    list.add(getString(R.string.mi_chongzhijine));
                                    list.add(getString(R.string.mi_duihuanjinbi));
                                    fragmentlist.add(new SeaPayFragment());
                                    fragmentlist.add(new LiveCoinFragment());
                                    zhubooryonghu.setText(R.string.mi_jinbiyue);
                                    tv_coincount.setText(coinBean.getData().getCoin() + "");
                                    tv_jifen.setText(getString(R.string.mi_wodejifen2) + coinBean.getData().getCredit());
                                } else {
                                    list.add(getString(R.string.mi_chongzhijine));
                                    list.add(getString(R.string.mi_jifentixian));
                                    fragmentlist.add(new SeaPayFragment());
                                    fragmentlist.add(new JifentixianFragment());
                                    zhubooryonghu.setText(R.string.mi_dangqianketixianjifen);
                                    tv_coincount.setText("" + coinBean.getData().getCredit());
                                    tv_jifen.setText(getString(R.string.mi_jinbiyue2) + coinBean.getData().getCoin());
                                }


                                HomepageAdapter pagerAdapter = new HomepageAdapter(getSupportFragmentManager(), fragmentlist, list);
                                vp_wallet.setAdapter(pagerAdapter);
                                tab_home.setupWithViewPager(vp_wallet);

                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });


    }
}